package cn.bdqn.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bdqn.entity.AppCategory;
import cn.bdqn.entity.AppInfo;
import cn.bdqn.entity.DataDictionary;
import cn.bdqn.service.AppCategoryService;
import cn.bdqn.service.AppInfoService;
import cn.bdqn.service.AppVersionService;
import cn.bdqn.service.DictionaryService;
import cn.bdqn.utils.PageBean;
//backendApp/appcheck
@Controller
@RequestMapping("/backendApp")
public class backendAppController {
	// -------------------------------------
	// 字典表的service层
	@Autowired
	private DictionaryService dictionaryService;

	// 分类表的service层
	@Autowired
	private AppCategoryService appCategoryService;

	// app信息的service
	@Autowired
	private AppInfoService appInfoService;

	// app版本信息的service
	@Autowired
	private AppVersionService appVersionService;

	// ------------------------------------
	@RequestMapping("/main")
	public String main() throws Exception {
		return "backend/main";
	}

	//审核操作   action="checksave" metho
	@RequestMapping("/checksave")
	public String checkSave(AppInfo appInfo) throws Exception{
	int num=appInfoService.updatestatus(appInfo.getStatus(),appInfo.getId());
		if(num>0){
			//成功
			return "redirect:/backendApp/list/1";
		}else{
			//失败。
			return "redirect:/backend/appcheck/"+appInfo.getStatus();
		}
	}
	public void show(){
		
		System.out.println("aa");

	}
	
	//  <a href="${}/backendApp/appcheck" class="btn btn-danger">审核</a>
	/**
	 * 数据回显 准备审核。
	 * @return
	 */
	@RequestMapping("/appcheck/{id}")
	public String appcheck(@PathVariable("id")Integer id,Model model) throws Exception{
		//1.通过id查询app的基础信息
				AppInfo appInfo = appInfoService.findAppInfoById(id);
				//1.放到model中
				model.addAttribute("appInfo", appInfo);
				//2.所属平台
				List<DataDictionary> flatformList = dictionaryService.findAllStatus("APP_FLATFORM");
				//2.放到model中
				model.addAttribute("flatformList", flatformList);
				//3.查询1级分类
				List<AppCategory> level1List = appCategoryService.findAllLevelOne();
				//3.放到model中
				model.addAttribute("level1List", level1List);
				//4.为了二级和三级回显 将二级三级从新查询出来
				List<AppCategory> level2List = findLevelNoJson(appInfo.getCategorylevel1()+"");
				List<AppCategory> level3List = findLevelNoJson(appInfo.getCategorylevel2()+"");
				//4.放入model中
				model.addAttribute("level2List", level2List);
				model.addAttribute("level3List", level3List);
				
		return "backend/appcheck";
	}
	
	/**
	 * 显示所有app
	 * 
	 * @return
	 */
	@RequestMapping("/list/{status}")
	public String appInfoList(
			Model model,
			@RequestParam(value = "softwareName", required = false) String checkSoftwareName,
			@PathVariable("status") String status,
			@RequestParam(value = "flatform", required = false) String checkFlatform,
			@RequestParam(value = "level1", required = false) String checkLevel1,
			@RequestParam(value = "level2", required = false) String checkLevel2,
			@RequestParam(value = "level3", required = false) String checkLevel3,
			@RequestParam(value = "currentPage", required = false) String currentPage,
			@RequestParam(value = "pageSize", required = false) String pageSize) 
					throws Exception{
		// 1.app状态
		List<DataDictionary> statusList = dictionaryService
				.findAllStatus("APP_STATUS");
		// 1.放到model中
		model.addAttribute("statusList", statusList);
		// 2.所属平台
		List<DataDictionary> flatformList = dictionaryService
				.findAllStatus("APP_FLATFORM");
		// 2.放到model中
		model.addAttribute("flatformList", flatformList);
		// 3.查询1级分类
		List<AppCategory> level1List = appCategoryService.findAllLevelOne();
		// 3.放到model中
		model.addAttribute("level1List", level1List);
		// 4.查询所有app的信息
		// 4.1封装查询数据
		Map<String, Object> checkMap = new HashMap<>();
		checkMap.put("softwareName", checkSoftwareName);
		checkMap.put("status", status);
		checkMap.put("flatform", checkFlatform);
		checkMap.put("level1", checkLevel1);
		checkMap.put("level2", checkLevel2);
		checkMap.put("level3", checkLevel3);
		checkMap.put("currentPage", currentPage);
		checkMap.put("pageSize", pageSize);
		PageBean<AppInfo> pageBean = appInfoService.findAll(checkMap);
		// 4.放到model中
		model.addAttribute("pageBean", pageBean);
		// 5.为了数据回显,讲map放到model中
		model.addAttribute("checkMap", checkMap);
		// 6.为了二级和三级回显 将二级三级从新查询出来
		List<AppCategory> level2List = findLevelNoJson(checkLevel1);
		List<AppCategory> level3List = findLevelNoJson(checkLevel2);
		// 6.放入model中
		model.addAttribute("level2List", level2List);
		model.addAttribute("level3List", level3List);
		return "backend/appInfoList";
	}

	// 二级 三级。
	public List<AppCategory> findLevelNoJson(String parentId) throws Exception {
		// 调用方法查询
		List<AppCategory> levelList = appCategoryService
				.findAllLevelByParentId(parentId);
		return levelList;
	}

}
